package com.wgod.demo.business.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgod.demo.business.entity.CommentEntity;
import com.wgod.demo.business.entity.ContentEntity;
import com.wgod.demo.business.entity.RelationShipEntity;
import com.wgod.demo.business.mapper.CommentMapper;
import com.wgod.demo.business.mapper.ContentMapper;
import com.wgod.demo.business.mapper.RelationShipMapper;
import com.wgod.demo.business.model.cond.ContentCond;
import com.wgod.demo.business.model.cond.MetaCond;
import com.wgod.demo.common.constant.ErrorConstant;
import com.wgod.demo.common.constant.Types;
import com.wgod.demo.common.constant.WebConst;
import com.wgod.demo.common.exception.BusinessException;

import com.wgod.security.common.biz.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by WGod on 2018/4/29.
 */
@Service
public class ContentService extends BaseService<ContentMapper,ContentEntity> {


    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private MetaService metaService;

    @Autowired
    private RelationShipMapper relationShipMapper;


    @Transactional
    public void addArticle(ContentEntity contentEntity) {
        if (null == contentEntity)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        if (StringUtils.isBlank(contentEntity.getTitle()))
            throw BusinessException.withErrorCode(ErrorConstant.Article.TITLE_CAN_NOT_EMPTY);
        if (contentEntity.getTitle().length() > WebConst.MAX_TITLE_COUNT)
            throw BusinessException.withErrorCode(ErrorConstant.Article.TITLE_IS_TOO_LONG);
        if (StringUtils.isBlank(contentEntity.getContent()))
            throw BusinessException.withErrorCode(ErrorConstant.Article.CONTENT_CAN_NOT_EMPTY);
        if (contentEntity.getContent().length() > WebConst.MAX_TEXT_COUNT)
            throw BusinessException.withErrorCode(ErrorConstant.Article.CONTENT_IS_TOO_LONG);

        //标签和分类
        String tags = contentEntity.getTags();
        String categories = contentEntity.getCategories();

        mapper.addArticle(contentEntity);

        int cid = contentEntity.getCid();
        metaService.addMetas(cid, tags, Types.TAG.getType());
        metaService.addMetas(cid, categories, Types.CATEGORY.getType());
    }

    @Transactional
    public void deleteArticleById(Integer cid) {
        if (null == cid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        mapper.deleteArticleById(cid);
        //同时也要删除该文章下的所有评论
        List<CommentEntity> comments = commentMapper.getCommentsByCId(cid);
        if (null != comments && comments.size() > 0){
            comments.forEach(comment ->{
                commentMapper.deleteComment(comment.getCoid());
            });
        }
        //删除标签和分类关联
        List<RelationShipEntity> relationShips = relationShipMapper.getRelationShipByCid(cid);
        if (null != relationShips && relationShips.size() > 0){
            relationShipMapper.deleteRelationShipByCid(cid);
        }

    }

    @Transactional
    public void updateArticleById(ContentEntity contentEntity) {
        //标签和分类
        String tags = contentEntity.getTags();
        String categories = contentEntity.getCategories();

        mapper.updateArticleById(contentEntity);
        int cid = contentEntity.getCid();
        relationShipMapper.deleteRelationShipByCid(cid);
        metaService.addMetas(cid, tags, Types.TAG.getType());
        metaService.addMetas(cid, categories, Types.CATEGORY.getType());

    }

    @Transactional
    public void updateCategory(String ordinal, String newCatefory) {
        ContentCond cond = new ContentCond();
        cond.setCategory(ordinal);
        List<ContentEntity> atricles = mapper.getArticlesByCond(cond);
        atricles.forEach(atricle -> {
            atricle.setCategories(atricle.getCategories().replace(ordinal, newCatefory));
            mapper.updateArticleById(atricle);
        });
    }



    public void updateContentByCid(ContentEntity content) {
        if (null != content && null != content.getCid()) {
            mapper.updateArticleById(content);
        }
    }

    public ContentEntity getAtricleById(Integer cid) {
        if (null == cid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        return mapper.getArticleById(cid);
    }

    public PageInfo<ContentEntity> getArticlesByCond(ContentCond contentCond, int pageNum, int pageSize) {
        if (null == contentCond)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        PageHelper.startPage(pageNum, pageSize);
        List<ContentEntity> contents = mapper.getArticlesByCond(contentCond);
        PageInfo<ContentEntity> pageInfo = new PageInfo<>(contents);
        return pageInfo;
    }

    public PageInfo<ContentEntity> getRecentlyArticle(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContentEntity> recentlyArticle = mapper.getRecentlyArticle();
        PageInfo<ContentEntity> pageInfo = new PageInfo<>(recentlyArticle);
        return pageInfo;
    }

    public PageInfo<ContentEntity> searchArticle(String param, int pageNun, int pageSize) {
        PageHelper.startPage(pageNun,pageSize);
        List<ContentEntity> contentEntitys = mapper.searchArticle(param);
        PageInfo<ContentEntity> pageInfo = new PageInfo<>(contentEntitys);
        return pageInfo;
    }
}
